<?php

class AreaController extends AppController {

	public $name = 'area';
	
	function beforeFilter() {
	    parent::beforeFilter(); 
		$this->C_auth->autoRedirect = false; //For override login
	    $this->C_auth->allowedActions = array('index','add','edit','del');

	}
	public function index($parent_id=0,$area_type=0) {
        if($parent_id<=0||!$parent_id){
	        $parent_id=0;
        } 		
        if($area_type<=0||!$area_type){
	        $area_type=0;
        } 
		
		$query = array(
			'conditions' => array('area.parent_id' => $parent_id), 
			'fields' => array('area.*'), 
		);
	    $areas_info = POP::exe('[@@:UM area][@@:QU find:all]',$query);		
		$query = array(
			'conditions' => array('area.id' => $parent_id), 
			'fields' => array('area.*'), 
		);
	    $parent_id_last = POP::exe('[@@:UM area][@@:QU find:first]',$query);			
        $this->set('parent_id_last',$parent_id_last);		
        $this->set('parent_id',$parent_id);
        $this->set('area_type',$area_type);
        $this->set('areas_info',$areas_info);      		
	}	
	public function add() {	
        if (Request::isAjax()) { 
            header('Content-Type: text/html; charset='.Param::read('App.encoding'));			
		    $id = Request::$data['id'];
			$v = Request::$data['v'];
			$query['keyid'] = $id;
			$query['set']['area']= array('area_name' => iconv('UTF-8', 'GB2312', $v));			
			if (POP::exe('[@@:UM area][@@:QU save]',$query)) {				
			    echo 1;
			}else {
			    echo 0 ;
			}
			exit;
		}
		if (!empty($this->data)) {
	        $this->data['area']['parent_id'] = Request::$query['parent_id'];
	        $this->data['area']['area_type'] = Request::$query['area_type'];					
			$query = $this->data;
			if (POP::exe('[@@:UM area][@@:QU save]',$query)) {
				$this->flash('更新成功！', Router::url('admin/area/index/'.Request::$query['parent_id'].'/'.Request::$query['area_type']));		
			} else {
                $this->flash('更新失败！', Router::url('admin/area/index/'.Request::$query['parent_id'].'/'.Request::$query['area_type']));
			} 	
	    }
	}
	public function edit() {

	}	
	public function del($id) {	
		$query = array(
			'conditions' => array('area.parent_id' => $id), 
			'fields' => array('area.*'), 
		);
	    $parent_id_last = POP::exe('[@@:UM area][@@:QU find:first]',$query);				
        if(isset($parent_id_last['area']['id'])) {
		    $this->flash('删除失败，请先删除该项的子项目！', Router::url('admin/area/index/'.Request::$query['parent_id'].'/'.Request::$query['area_type']));			 
		}else {
			$query = array('keyid'=>$id);			
			POP::exe('[@@:UM area][@@:QU del]',$query);			
		    $this->flash('删除成功！', Router::url('admin/area/index/'.Request::$query['parent_id'].'/'.Request::$query['area_type']));
        }		

	}	
}
?> 